﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using EnvDTE;
using EnvDTE80;

namespace RunVistaGadget31.AdditionalApplicationsWrappers
{
    /// <summary>
    /// 
    /// </summary>
    public class CabarcRunner : ApplicationRunner
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationObject"></param>
        public CabarcRunner(DTE2 applicationObject)
            : base(applicationObject, false)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="applicationObject"></param>
        /// <param name="isRunAsWinApp"></param>
        public CabarcRunner(DTE2 applicationObject, bool isRunAsWinApp)
            : base(applicationObject, isRunAsWinApp)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string GetAppArguments()
        {
            String RootDirectory = Path.GetDirectoryName(project.FullName);
            String RootDirectoryWithoutDiskName = RootDirectory.Replace(Path.GetPathRoot(RootDirectory), "") + "\\";
            String AllowedFileExtensions = GadgetProp.AllowedExtensionsForGadget;
            String[] AllowesFileExtensionsArray = AllowedFileExtensions.Split(';');
            StringBuilder AllowedInGadgetFileExtensions = new StringBuilder();
            for (int i = 0; i < AllowesFileExtensionsArray.Length; i++)
            {
                if (!String.IsNullOrEmpty(AllowesFileExtensionsArray[i].Trim()))
                {
                    AllowedInGadgetFileExtensions.Append(String.Format(" {0}\\{1}", RootDirectory, AllowesFileExtensionsArray[i].Trim()));
                }

            }

            String CabarcArgs = @" -r -p -P " + RootDirectoryWithoutDiskName + " n " + GadgetPath + AllowedInGadgetFileExtensions.ToString();
            return CabarcArgs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string GetOutputAndRunApplication()
        {
            GadgetProperties gp = new GadgetProperties();
            base.CalledApplicationPath = gp.ArchivatorPath;
            if (base.IsExistCalledApplication(base.CalledApplicationPath, "CABARC.EXE") == false)
                throw new Exception("Could not find required application CABARC.EXE. Please, define it with 'GadgetProperties' form.");
            return base.GetOutputAndRunApplication();
        }

    }
}
